<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/2
 * Time: 11:03
 */

namespace app\admin\controller;
use app\admin\model\Party as PartyModel;
/**财务公开控制器
 * Class Finance
 * @package app\admin\controller
 */
class Finance extends Admin
{
    protected $type = 3;
    /**首页
     * @return mixed
     */
    public function index()
    {
        $map = array(
            'status' => 0,  // 删除 未删除
            'type'=>$this->type,
        );
        $list = $this->lists('Party', $map);
        int_to_string($list, array(
            'recommend'=>array(0=>'不推荐',1=>'推荐'),
            'push'=>array(0=>'不推送',1=>'推送',2=>'已推送'),
        ));
        $this->assign([
            'list'=>$list,
            'type'=>$this->type,
        ]);
        return $this->fetch('party/index');
    }

    /*
     * 增加
     */
    public function add()
    {
        if (IS_POST) {
            $data = input('post.');
            $data['create_user'] = $_SESSION['think']['user_auth']['id'];
            if (empty($data['id'])) {
                unset($data['id']);
            }
            $data['type'] = $this->type;
            $Model = new PartyModel();
            $info = $Model->validate('Party')->save($data);
            if ($info) {
                return $this->success("新增成功", Url('Finance/index'));
            } else {
                return $this->error($Model->getError());
            }
        } else {
            $this->default_pic();
            $this->assign([
                'msg'=>'',
                'type'=>$this->type,
            ]);
            return $this->fetch('party/edit');
        }
    }

    /*
     * 编辑
     */
    public function edit()
    {
        $Model = new PartyModel();
        if (IS_POST) {
            $data = input('post.');
            $info = $Model->validate('Party')->save($data, ['id' => input('id')]);
            if ($info) {
                return $this->success("修改成功", Url("Finance/index"));
            } else {
                return $this->get_update_error_msg($Model->getError());
            }
        } else {
            $this->default_pic();
            $id = input('id');
            $msg = PartyModel::get($id);
            $this->assign([
                'msg'=>$msg,
                'type'=>$this->type,
            ]);
            return $this->fetch('party/edit');
        }
    }

    /*
     * 删除
     */
    public function del()
    {
        $id = input('id');
        $data['status'] = -1;
        $info = PartyModel::where('id', $id)->update($data);
        if ($info) {
            return $this->success("删除成功");
        } else {
            return $this->error("删除失败");
        }
    }
}